# -*- Python -*- vim: set ft=python ts=4 sw=4 expandtab tw=79:

#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#

# Configuration file for the 'lit' test runner.

import ctypes
import lit.formats
import os
import re
import subprocess

# Tell pylint that we know config and lit_config exist somewhere.
if 'PYLINT_IMPORT' in os.environ:
    config = object()
    lit_config = object()

def append_dynamic_library_path(path):
    if config.operating_system == 'Windows':
        name = 'PATH'
        sep = ';'
    elif config.operating_system == 'Darwin':
        name = 'DYLD_LIBRARY_PATH'
        sep = ':'
    else:
        name = 'LD_LIBRARY_PATH'
        sep = ':'
    if name in config.environment:
        config.environment[name] = path + sep + config.environment[name]
    else:
        config.environment[name] = path

if(config.architecture == "x86_64"):
    lib = ctypes.cdll.LoadLibrary(config.libpgmath_getarchlib_dir + "/" + config.libpgmath_getarchlib)
    lib.get_arch.restype = ctypes.c_char_p
    arch = ctypes.c_char_p(lib.get_arch()).value.decode("utf-8")
    config.available_features.add(arch)

config.available_features.add(config.architecture)

# host_triple = getattr(config, 'host_triple', None)
# target_triple = getattr(config, 'target_triple', None)
# if host_triple and host_triple == target_triple:
config.available_features.add('native')

# name: The name of this test suite.
config.name = 'libpgmath'

# suffixes: A list of file extensions to treat as test files.
config.suffixes = ['.c']

# test_source_root: The root path where tests are located.
config.test_source_root = os.path.dirname(__file__)

# test_exec_root: The root object directory where output is placed
config.test_exec_root = config.libpgmath_obj_root

# test format
config.test_format = lit.formats.ShTest()

if config.operating_system == 'Windows':
    append_dynamic_library_path(config.libpgmath_runtime_dir)

# compiler flags
config.test_cflags = ""
if config.operating_system != 'Windows':
    config.test_cflags += " -g"
else:
    config.test_cflags += " -DTARGET_WIN"

if config.architecture == "x86_64":
    if config.operating_system == 'Darwin':
        config.test_cflags += " -DTARGET_OSX_X8664"
    elif config.operating_system == 'Windows':
        config.test_cflags += " -DTARGET_WIN_X8664"
    elif config.operating_system == 'Linux':
        config.test_cflags += " -DTARGET_LINUX_X8664"

config.test_cflags += " -O1 " \
                     "-I" + config.test_source_root

libs = ""
if config.has_libpgmath_runtime:
    if config.operating_system != 'Windows':
        libs += " " + os.path.join(config.libpgmath_runtime_dir,
            config.libpgmath_runtime) + \
            " -Wl,-rpath," + config.libpgmath_runtime_dir
    else:
        libs += " " + os.path.join(config.libpgmath_runtime_dir,
            config.libpgmath_runtime.replace(".dll", "") + ".lib")

if config.has_libm and config.operating_system != 'Windows':
    libs += " -lm"

config.test_compiler = "\"" + config.test_compiler + "\""

config.ompt_test_compiler = config.test_compiler

if(config.architecture == "x86_64"):
    config.test_cflags += " -march=native"
elif(config.architecture == "ppc64le"):
    config.test_cflags += " -mcpu=native"

config.substitutions.append(("%libpgmath-compile", \
    "%compiler %cflags %s -o %t " + libs))
config.substitutions.append(("%libpgmath-run", "%t"))
config.substitutions.append(("%compiler", config.test_compiler))
config.substitutions.append(("%cflags", config.test_cflags))
